package default_package;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.sound.sampled.*;

public class ThreadSound implements Runnable {
	private InputStream in_Sound;
	private OutputStream out_SoundOppose;
	
	private BufferedReader in_confirm;
	private PrintWriter out_confirm;
	private PrintWriter out_confirm_oppose;
	
	public ThreadSound(InputStream inputStream, OutputStream outSoundCLientOppose, BufferedReader in_confirm, PrintWriter out_confirm , PrintWriter out_confirm_oppose) {
		super();
		this.in_Sound = inputStream;
		this.out_SoundOppose = outSoundCLientOppose;
		this.in_confirm = in_confirm;
		this.out_confirm = out_confirm;
		this.out_confirm_oppose = out_confirm_oppose;
	}
	
	private AudioFormat getAudioFormat() {
	    float sampleRate = 24000.0F;
	    int sampleSizeBits = 16;
	    int channels = 1;
	    boolean signed = true;
	    boolean bigEndian = true;
	    return new AudioFormat(sampleRate, sampleSizeBits, channels, signed, bigEndian);
	}
	
	public void run() {
		boolean quitter = false;
		int i = 1;
		while(!quitter) {
			File f = null;
			AudioInputStream audioInputStream = null;
			byte[] b = new byte[0];
			f = new File("Conversation-Audio-" + i);
			i++;
			
			AudioFormat audioFormat = getAudioFormat();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			try {
				Thread tPasser = new Thread(new ThreadPasserLaMain(in_confirm));
				tPasser.start();
				while(tPasser.isAlive()) {
					b = new byte[1024];
					in_Sound.read(b, 0, 1024);
					baos.write(b.clone(), 0, b.length);
					out_SoundOppose.write(b,0, b.length);
					out_SoundOppose.flush();
				}
				
				ByteArrayInputStream byteArray = new ByteArrayInputStream(baos.toByteArray());
				try {
					audioInputStream = new AudioInputStream (byteArray, audioFormat, baos.toByteArray().length);
					AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, f);
				} catch (IOException e1) {
					e1.printStackTrace();
				} finally {
					try {
						audioInputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
				String ch = in_confirm.readLine();
				if(ch.equals("fini")) {
					out_confirm_oppose.println("fini");
					out_confirm_oppose.flush();
				} else if(ch.equals("quitter")){
					quitter = true;
					//System.out.println("quitter 1");
					out_confirm_oppose.println("quitter");
					out_confirm_oppose.flush();
					out_confirm.println("Finquitter");
					out_confirm.flush();
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					b = new byte[1024];
					b[0] = 1;
					out_SoundOppose.write(b,0, b.length);
					out_SoundOppose.flush();
				}
				else
				{
					//System.out.println("quitter 2");
					quitter = true;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//System.out.println("je suis sorti de Thread Sound");
	}
}